package com.shrct.businessprocess.project.test.impl;

import com.shrct.businessprocess.budget.RCCostBreakDown;
import com.shrct.businessprocess.common.CurrencyType;
import com.shrct.businessprocess.organization.RCEmployee;
import com.shrct.businessprocess.organization.RCEmployeeDAO;
import com.shrct.businessprocess.organization.RCEnterprise;
import com.shrct.businessprocess.organization.RCOrganizationService;
import com.shrct.businessprocess.product.RCBrand;
import com.shrct.businessprocess.product.RCProductSpecification;
import com.shrct.businessprocess.product.RCProductSpecificationDAO;
import com.shrct.businessprocess.product.RCServiceLevel;
import com.shrct.businessprocess.product.RCServiceResponseTime;
import com.shrct.businessprocess.project.RCCustomerContract;
import com.shrct.businessprocess.project.RCExchangeRate;
import com.shrct.businessprocess.project.RCInitializeProjectProcess;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.project.RCProjectDAO;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.project.RCProjectSaleAssignment;
import com.shrct.businessprocess.project.test.ProjectDataService;
import com.shrct.businessprocess.purchase.RCBudgetContract;
import com.shrct.businessprocess.purchase.RCBudgetContractProductItem;
import com.shrct.businessprocess.purchase.RCProductPurchaseBudget;
import com.shrct.businessprocess.purchase.RCProductPurchaseBudgetDAO;
import com.shrct.businessprocess.purchase.RCProductPurchaseRequest;
import com.shrct.businessprocess.purchase.RCProductPurchaseRequestDAO;
import com.shrct.businessprocess.purchase.RCProductPurchaseRequestItem;
import com.shrct.businessprocess.purchase.RCPurchaseRequestProcess;
import edu.fudan.langlab.domain.workflow.ProcessManager;
import edu.fudan.mylang.pf.BaseService;
import edu.fudan.mylang.pf.IGenericQuery;
import java.util.Collection;
import java.util.Date;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("com.shrct.businessprocess.project.test.ProjectDataService")
public class ProjectDataServiceImpl extends BaseService implements ProjectDataService {
  @Autowired
  private ProcessManager processManager;
  
  @Autowired
  private RCProjectDAO projectDAO;
  
  @Autowired
  private RCEmployeeDAO employeeDAO;
  
  @Autowired
  private RCOrganizationService organizationService;
  
  @Autowired
  private RCProductPurchaseBudgetDAO productPurchaseBudgetDAO;
  
  @Autowired
  private RCProductSpecificationDAO productSpecificationDAO;
  
  @Autowired
  private RCProductPurchaseRequestDAO productPurchaseRequestDAO;
  
  public void passProject(final RCProject project) {
    final RCInitializeProjectProcess projectProcess = this.projectDAO.createInitializeProjectProcess(project);
    projectProcess.SalesManagerAudit_agree("");
    projectProcess.CEOAudit_agree("");
  }
  
  public RCEmployee getDefaultSales() {
    RCEmployee _findByEid = this.employeeDAO.findByEid("C04");
    return _findByEid;
  }
  
  public RCEmployee getDefaultBusinessMan() {
    RCEmployee _findByEid = this.employeeDAO.findByEid("C14");
    return _findByEid;
  }
  
  public RCBrand getDefaultProductCategory() {
    RCBrand _xblockexpression = null;
    {
      final String codeid = "01";
      IGenericQuery<RCBrand> query=getObjectFactory().createGenericQuery(RCBrand.class,"select p from com.shrct.businessprocess.product.impl.RCBrandImpl as p   where ((p.codeid) = (:codeid)) and  p.active = 1      ").setParameter("codeid",codeid);
      RCBrand _uniqueResult = query.uniqueResult();
      _xblockexpression = (_uniqueResult);
    }
    return _xblockexpression;
  }
  
  public RCEnterprise getDefaultCustomer() {
    RCEnterprise _findEnterpriseByCode = this.organizationService.findEnterpriseByCode("0000002");
    return _findEnterpriseByCode;
  }
  
  public RCProductSpecification getDefaultProductSpec() {
    RCProductSpecification _findByCode = this.productSpecificationDAO.findByCode("PCIS0001");
    return _findByCode;
  }
  
  public RCProductSpecification getDefaultFullServiceSpec() {IGenericQuery<RCServiceLevel> query=getObjectFactory().createGenericQuery(RCServiceLevel.class,"select e from com.shrct.businessprocess.product.impl.RCServiceLevelImpl as e   where  e.active = 1      ");
    Collection<RCServiceLevel> _list = query.list();
    final RCServiceLevel serviceLevel = IterableExtensions.<RCServiceLevel>head(_list);IGenericQuery<RCServiceResponseTime> query_1=getObjectFactory().createGenericQuery(RCServiceResponseTime.class,"select e from com.shrct.businessprocess.product.impl.RCServiceResponseTimeImpl as e   where  e.active = 1      ");
    Collection<RCServiceResponseTime> _list_1 = query_1.list();
    final RCServiceResponseTime serviceResponseTime = IterableExtensions.<RCServiceResponseTime>head(_list_1);
    RuntimeException _runtimeException = new RuntimeException("fix me");
    throw _runtimeException;
  }
  
  public RCProject getNewProject() {
    final Procedure1<RCProject> _function = new Procedure1<RCProject>() {
        public void apply(final RCProject project) {
          project.setName("TestName");
          CurrencyType _defaultCurrencyCode = ProjectDataServiceImpl.this.projectDAO.getDefaultCurrencyCode();
          project.setCurrencyType(_defaultCurrencyCode);
          RCExchangeRate _createAndAddtoExchangeRates = project.createAndAddtoExchangeRates();
          final Procedure1<RCExchangeRate> _function = new Procedure1<RCExchangeRate>() {
              public void apply(final RCExchangeRate it) {
                CurrencyType _currencyType = project.getCurrencyType();
                it.setCurrencyType(_currencyType);
                it.setRate(Double.valueOf(1.0));
              }
            };
          ObjectExtensions.<RCExchangeRate>operator_doubleArrow(_createAndAddtoExchangeRates, _function);
          RCProjectSaleAssignment _createAndAddtoSalers = project.createAndAddtoSalers();
          final Procedure1<RCProjectSaleAssignment> _function_1 = new Procedure1<RCProjectSaleAssignment>() {
              public void apply(final RCProjectSaleAssignment it) {
                RCEmployee _defaultSales = ProjectDataServiceImpl.this.getDefaultSales();
                it.setEmployee(_defaultSales);
                it.setRate(Double.valueOf(100.0));
                it.setIsMajor(Boolean.valueOf(true));
              }
            };
          ObjectExtensions.<RCProjectSaleAssignment>operator_doubleArrow(_createAndAddtoSalers, _function_1);
          ProjectDataServiceImpl.this.passProject(project);
        }
      };
    RCProject _createProject = this.projectDAO.createProject(_function);
    return _createProject;
  }
  
  public RCProductPurchaseBudget initWithProductPurchaseBudgetDAO(final RCProject project) {
    RCCostBreakDown _cbd = project.getCbd();
    final Procedure1<RCProductPurchaseBudget> _function = new Procedure1<RCProductPurchaseBudget>() {
        public void apply(final RCProductPurchaseBudget it) {
          RCBrand _defaultProductCategory = ProjectDataServiceImpl.this.getDefaultProductCategory();
          it.setProductCategory(_defaultProductCategory);
          RCProjectMoney _zeroAmount = project.getZeroAmount();
          final Procedure1<RCProjectMoney> _function = new Procedure1<RCProjectMoney>() {
              public void apply(final RCProjectMoney it) {
                it.setAmount(Double.valueOf(1000d));
              }
            };
          RCProjectMoney _doubleArrow = ObjectExtensions.<RCProjectMoney>operator_doubleArrow(_zeroAmount, _function);
          it.setServiceContractPrice(_doubleArrow);
          it.setServiceContractTaxRate(Double.valueOf(17d));
          RCProjectMoney _zeroAmount_1 = project.getZeroAmount();
          final Procedure1<RCProjectMoney> _function_1 = new Procedure1<RCProjectMoney>() {
              public void apply(final RCProjectMoney it) {
                it.setAmount(Double.valueOf(500d));
              }
            };
          RCProjectMoney _doubleArrow_1 = ObjectExtensions.<RCProjectMoney>operator_doubleArrow(_zeroAmount_1, _function_1);
          it.setServiceNetPrice(_doubleArrow_1);
          it.setServiceNetTaxRate(Double.valueOf(17d));
          RCProjectMoney _zeroAmount_2 = project.getZeroAmount();
          final Procedure1<RCProjectMoney> _function_2 = new Procedure1<RCProjectMoney>() {
              public void apply(final RCProjectMoney it) {
                it.setAmount(Double.valueOf(10000d));
              }
            };
          RCProjectMoney _doubleArrow_2 = ObjectExtensions.<RCProjectMoney>operator_doubleArrow(_zeroAmount_2, _function_2);
          it.setProductContractPrice(_doubleArrow_2);
          it.setProductContractTaxRate(Double.valueOf(6d));
          RCProjectMoney _zeroAmount_3 = project.getZeroAmount();
          final Procedure1<RCProjectMoney> _function_3 = new Procedure1<RCProjectMoney>() {
              public void apply(final RCProjectMoney it) {
                it.setAmount(Double.valueOf(5000d));
              }
            };
          RCProjectMoney _doubleArrow_3 = ObjectExtensions.<RCProjectMoney>operator_doubleArrow(_zeroAmount_3, _function_3);
          it.setProductNetPrice(_doubleArrow_3);
          it.setProductNetTaxRate(Double.valueOf(6d));
        }
      };
    RCProductPurchaseBudget _create = this.productPurchaseBudgetDAO.create(_cbd, _function);
    return _create;
  }
  
  public RCCustomerContract initWithCustomerContract(final RCProject project) {
    final Procedure1<RCCustomerContract> _function = new Procedure1<RCCustomerContract>() {
        public void apply(final RCCustomerContract contract) {
          contract.setContractId("001");
          RCEnterprise _defaultCustomer = ProjectDataServiceImpl.this.getDefaultCustomer();
          contract.setCustomer(_defaultCustomer);
        }
      };
    RCCustomerContract _createContract = project.createContract(_function);
    final Procedure1<RCCustomerContract> _function_1 = new Procedure1<RCCustomerContract>() {
        public void apply(final RCCustomerContract it) {
          it.populateToAllBudget();
        }
      };
    RCCustomerContract _doubleArrow = ObjectExtensions.<RCCustomerContract>operator_doubleArrow(_createContract, _function_1);
    return _doubleArrow;
  }
  
  public RCBudgetContractProductItem initWithRequiredProductList(final RCProductPurchaseBudget budget) {
    RCBudgetContractProductItem _xblockexpression = null;
    {
      Collection<RCBudgetContract> _contracts = budget.getContracts();
      RCBudgetContract _head = IterableExtensions.<RCBudgetContract>head(_contracts);
      RCBudgetContractProductItem _createProductItem = _head.createProductItem();
      final Procedure1<RCBudgetContractProductItem> _function = new Procedure1<RCBudgetContractProductItem>() {
          public void apply(final RCBudgetContractProductItem it) {
            RCProductSpecification _defaultProductSpec = ProjectDataServiceImpl.this.getDefaultProductSpec();
            it.setProductSpec(_defaultProductSpec);
            it.setQuantity(Integer.valueOf(5));
            RCCostBreakDown _cbd = budget.getCbd();
            RCProject _project = _cbd.getProject();
            RCProjectMoney _zeroAmount = _project.getZeroAmount();
            final Procedure1<RCProjectMoney> _function = new Procedure1<RCProjectMoney>() {
                public void apply(final RCProjectMoney it) {
                  it.setAmount(Double.valueOf(10001d));
                }
              };
            RCProjectMoney _doubleArrow = ObjectExtensions.<RCProjectMoney>operator_doubleArrow(_zeroAmount, _function);
            it.setPrice(_doubleArrow);
          }
        };
      final RCBudgetContractProductItem productItem = ObjectExtensions.<RCBudgetContractProductItem>operator_doubleArrow(_createProductItem, _function);
      _xblockexpression = (productItem);
    }
    return _xblockexpression;
  }
  
  public RCProductPurchaseRequest initWithPurchaseRequest(final RCProductPurchaseBudget budget) {
    RCProductPurchaseRequest _createProductPurchaseRequest = budget.createProductPurchaseRequest();
    final Procedure1<RCProductPurchaseRequest> _function = new Procedure1<RCProductPurchaseRequest>() {
        public void apply(final RCProductPurchaseRequest it) {
          Date _date = new Date();
          it.setDate(_date);
          Date _date_1 = new Date();
          it.setExpectDate(_date_1);
          RCCostBreakDown _cbd = budget.getCbd();
          RCProject _project = _cbd.getProject();
          RCProjectMoney _zeroAmount = _project.getZeroAmount();
          final Procedure1<RCProjectMoney> _function = new Procedure1<RCProjectMoney>() {
              public void apply(final RCProjectMoney it) {
                it.setAmount(Double.valueOf(11000d));
              }
            };
          RCProjectMoney _doubleArrow = ObjectExtensions.<RCProjectMoney>operator_doubleArrow(_zeroAmount, _function);
          it.setExpectCost(_doubleArrow);
          RCProductPurchaseRequestItem _createAndAddtoItems = it.createAndAddtoItems();
          final Procedure1<RCProductPurchaseRequestItem> _function_1 = new Procedure1<RCProductPurchaseRequestItem>() {
              public void apply(final RCProductPurchaseRequestItem it) {
                RCProductSpecification _defaultProductSpec = ProjectDataServiceImpl.this.getDefaultProductSpec();
                it.setProductSpec(_defaultProductSpec);
                it.setQuantity(Integer.valueOf(5));
              }
            };
          ObjectExtensions.<RCProductPurchaseRequestItem>operator_doubleArrow(_createAndAddtoItems, _function_1);
          RCProductPurchaseRequestItem _createAndAddtoItems_1 = it.createAndAddtoItems();
          final Procedure1<RCProductPurchaseRequestItem> _function_2 = new Procedure1<RCProductPurchaseRequestItem>() {
              public void apply(final RCProductPurchaseRequestItem it) {
                RCProductSpecification _defaultFullServiceSpec = ProjectDataServiceImpl.this.getDefaultFullServiceSpec();
                it.setProductSpec(_defaultFullServiceSpec);
                it.setQuantity(Integer.valueOf(5));
              }
            };
          ObjectExtensions.<RCProductPurchaseRequestItem>operator_doubleArrow(_createAndAddtoItems_1, _function_2);
        }
      };
    RCProductPurchaseRequest _doubleArrow = ObjectExtensions.<RCProductPurchaseRequest>operator_doubleArrow(_createProductPurchaseRequest, _function);
    final Procedure1<RCProductPurchaseRequest> _function_1 = new Procedure1<RCProductPurchaseRequest>() {
        public void apply(final RCProductPurchaseRequest it) {
          RCPurchaseRequestProcess _startPurchaseProcess = ProjectDataServiceImpl.this.productPurchaseRequestDAO.startPurchaseProcess(it);
          final Procedure1<RCPurchaseRequestProcess> _function = new Procedure1<RCPurchaseRequestProcess>() {
              public void apply(final RCPurchaseRequestProcess it) {
                it.SaleManagerAudit_agree("");
                it.CEOAudit_agree("");
              }
            };
          ObjectExtensions.<RCPurchaseRequestProcess>operator_doubleArrow(_startPurchaseProcess, _function);
        }
      };
    RCProductPurchaseRequest _doubleArrow_1 = ObjectExtensions.<RCProductPurchaseRequest>operator_doubleArrow(_doubleArrow, _function_1);
    return _doubleArrow_1;
  }
}
